﻿using System;
using EmbeddedSharp;
using EmbeddedSharp.Core;

namespace EmbeddedSharp.Scripting {
    public class VariableNode : IdentifierNode {

        public IContext Context {
            get;
            set;
        }

        public string Name
        {
            get;
            set;
        }

        public VariableNode(string name)
        {
            this.Name = name;
        }

        /// <summary>
        /// Get variable or function node from the context
        /// </summary>
        /// <returns></returns>
        public override object Evaluate() {
            //return this.Variable.Value;
            object node = Context.GetVariable(
                Convert.ToString(this.Name));
            if (node == null) {
                node = Context.GetFunctionManager().GetFunction(this.Name);
            }

            return node;
        }

        public override object Accept(IVisitor visitor) {
            return visitor.Visit(this);
        }
    }
}
